<template>
  <div id="map"></div>
  <ToolBoxIndex class="tool-box" :map="mapInstance" />
  <MapLayerTree v-if="map" :map="map" />
  <div class="btn-group">
    
    <div class="sub-buttons">
      <button class="sub-button" @click="setBaseMap('mapBase')" >
        基础地图
      </button>
      <button class="sub-button" @click="setBaseMap('Satellitemap')">
        卫星地图
      </button>
      <button class="sub-button" @click="setBaseMap('darkmap')">
        灰色地图
      </button>
      <button class="main-button"></button>
      
    </div>
  </div>
  <vue-draggable-resizable
    :w="100"
    :h="40"
    :x="initialX"
    :y="initialY"
    class="draggable-button"
    @dragging="onDrag"
  >
    <el-button
      type="primary"
      circle
      class="circle-button"
      @click="handleClick"
      :disabled="isDragging"
      v-if="!$store.eventTableShow"
    >
      <i class="iconfont icon-biaoge" style="font-size: 32px;"></i>
    </el-button>
  </vue-draggable-resizable>
  <div id="mouse-position">{{ mouseCoordinate }}</div>
</template>

<script setup>
import { ref, onMounted, inject, provide } from 'vue';
import MapLayerTree from './MapLayerTree.vue';
import ToolBoxIndex from './Toolbox/ToolBoxIndex.vue';
import ScaleLine from 'ol/control/ScaleLine'; // 引入 ScaleLine 控件
import VueDraggableResizable from 'vue-draggable-resizable';
import { useMapStore } from '../stores/map.js';
import { app } from '../main.js';

const $store = useMapStore();

let map = null; // 非响应式的 map 变量
let mapInstance = ref(null);
// 记录按钮是否正在拖拽
const isDragging = ref(false);
const onDragStart = () => {
  console.log(123)
  isDragging.value = true;
};
const onDragEnd = () => {
  isDragging.value = false;
};
const mouseCoordinate = ref(null); // 定义响应式变量来存储坐标

const mapBase = new ol.layer.Tile({
  title: '基础地图',
  source: new ol.source.XYZ({
    url: 'https://tile.openstreetmap.bzh/br/{z}/{x}/{y}.png',
    wrapX: false,
    crossOrigin: 'Anonymous'
  }),
});
const Satellitemap = new ol.layer.Tile({
  title: '卫星地图',
  source: new ol.source.XYZ({
    url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
    wrapX: false,
    crossOrigin: 'Anonymous'
  }),
});

const darkmap = new ol.layer.Tile({
  title: '灰色地图',
  source: new ol.source.XYZ({
    url: 'https://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/WMTS/tile/1.0.0/Canvas_World_Light_Gray_Base/default/default028mm/{z}/{y}/{x}/',
    wrapX: false,
    crossOrigin: 'Anonymous'
  }),
});

const docLayer = inject('docLayer');
const setBaseMap = (mapType) => {
  let layer;
  if (mapType === 'mapBase') {
    layer = mapBase;
  } else if (mapType === 'Satellitemap') {
    layer = Satellitemap;
  } else if (mapType === 'darkmap') {
    layer = darkmap;
  }
  if (layer) {
    map.getLayers().clear();
    map.addLayer(layer);
    map.addLayer(docLayer);
  }
};

/* 比例尺 */
const scaleLineControl = new ScaleLine({
  units: 'metric', // 可以选择 'metric'（公制）, 'imperial'（英制）, 或 'us'（美国习惯单位）
});

/* 鹰眼 */
const overviewMapControl = new ol.control.OverviewMap({
  /* 鹰眼控件样式  */
  className: 'ol-overviewmap ol-custom-overviewmap',
  layers: [mapBase],
  /* 鹰眼控件展开时功能按钮上的标识(网页的JS的字符编码) */
  collapseLabel: '\u00BB',
  /* 鹰眼控件折叠时功能按钮上的标识(网页的JS的字符编码) */
  label: '\u00AB',
  /* 初始为展开显示方式 */
  collapsed: false,
  view: new ol.View({
    projection: 'EPSG:4326',
    minZoom: 8,
    maxZoom: 18,
  }),
});

const zoomslider = new ol.control.ZoomSlider();
const mousePositionControl = new ol.control.MousePosition({
  /* 坐标格式 */
  coordinateFormat: ol.coordinate.createStringXY(4),
  projection: 'EPSG:4326',
  /* 坐标信息显示样式类名,默认是'ol-mouse-position' */
  className: 'custom-mouse-position',
  target: document.getElementById('mouse-position'),
  undefinedHTML: '&nbsp',
});

onMounted(() => {
  map = new ol.Map({
    target: 'map',
    layers: [mapBase],
    view: new ol.View({
      center: [114.37, 30.495],
      zoom: 14,
      minZoom: 12,
      maxZoom: 18,
      projection: 'EPSG:4326',
    }),
    controls: new ol.control.defaults({
      zoom: false, // 移除默认的缩放控件
    }),
  });

  if (docLayer) {
    map.addLayer(docLayer);
  }
  /* 禁止双击放大 */
  map.getInteractions().forEach((interaction) => {
  if (interaction instanceof ol.interaction.DoubleClickZoom) {
    map.removeInteraction(interaction);
  }
});
/* 添加控件 */
  map.addControl(scaleLineControl);
  map.addControl(overviewMapControl);
  map.addControl(zoomslider);
  map.addControl(mousePositionControl);

  // 添加 pointermove 事件监听器
  map.on("pointermove", evt => {
    mouseCoordinate.value = evt.coordinate; // 更新响应式变量的值
  });

  app.config.globalProperties.$map = map;
  //将docLayer对象挂载到全局
  app.config.globalProperties.$docLayer = docLayer;
  mapInstance.value = map;
  provide('map', map);
  provide('mapInstance', mapInstance);
});

// 可拖拽按钮
const initialX = ref(0);
const initialY = ref(0);
const onDrag = (x, y) => {
  initialX.value = x;
  initialY.value = y;
};

const handleClick = () => {
  if (!isDragging.value) {
    $store.setEventTableShow(true);
  }
};
</script>

<style scoped>
#map {
  width: 100vw;
  height: 100vh;
}

.tool-box {
  position: absolute;
  bottom: 180px;
  left: 10px;
  z-index: 1000;
}

.ol-custom-overviewmap {
  /* position: fixed; */
  right: 0;
  bottom: 0;
  top: auto;
  left: auto;
}

.ol-overviewmap-box {
  border: 1px solid #ff2d51;
}

.btn-group {
  position: absolute;
  right: 150px;
  bottom: 475px;
  display: inline-block;
}

.sub-buttons {
  position: absolute;
  display: flex;
  flex-direction: column; /* 修改为纵向排列 */
  align-items: center;
  z-index: 1;
  gap: 10px;
}

.main-button {
  width: 100px;
  height: 100px;
  border: none;
  border-radius: 5px;
  background-color: #fff;
  color: #000;
  cursor: pointer;
  position: relative;
  z-index: 2;
  opacity: 1;
  transform: translateY(0);
  transition: opacity 0.3s, transform 0.3s;
  background-image: url('../imgs/qiehuan.png');
  background-size: contain;
  border: 1px solid rgb(255, 255, 255);
}

.sub-button {
  width: 100px;
  height: 100px;
  border: none;
  border-radius: 5px;
  color: #000;
  cursor: pointer;
  opacity: 0;
  transform: translateY(-100%);
  transition: opacity 0.3s, transform 0.3s;
}

.sub-button:nth-child(1) {
  background-image: url('../imgs/base.png'); /* 基础地图背景图片 */
  background-size: cover;
  color: #000;
  z-index: 1;
}

.sub-button:nth-child(2) {
  background-image: url('../imgs/weixing.png'); /* 卫星地图背景图片 */
  background-size: cover;
  color: white;
  z-index: 1;
}

.sub-button:nth-child(3) {
  background-image: url('../imgs/gray.png'); /* 灰色地图背景图片 */
  background-size: cover;
  z-index: 1;
}

.btn-group:hover .main-button {
  opacity: 0;
  z-index: 0;
  /* display: none; */
}

.btn-group:hover .sub-button {
  opacity: 1;
  transform: translateY(100px) translateX(0); 
}

.btn-group:hover .sub-button:nth-child(1) {
  transition-delay: 0.1s;
}

.btn-group:hover .sub-button:nth-child(2) {
  transition-delay: 0.2s;
}

.btn-group:hover .sub-button:nth-child(3) {
  transition-delay: 0.3s;
}

.draggable-button {
  position: fixed;
  right: 100px;
  top: 30%;
  transform: translateY(-50%);
}

.circle-button {
  width: 100px;
  height: 100px;
  font-size: 24px;
  color: #333333;
  background-color: #ffcc80;
  opacity: 0.7;
  border: none;
  border-radius: 50%;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.circle-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.1);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.circle-button:hover::before {
  opacity: 1;
}

.circle-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2);
}

.circle-button:active {
  transform: translateY(1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

#mouse-position {
  position: absolute;
  bottom: 10px;
  right: 40%;
  z-index: 1000;
  background-color: rgba(255, 255, 255, 0.8);
  padding: 5px;
  border-radius: 5px;
  font-size: 15px;
}

.custom-mouse-position {
  color: #ff2d51;
  font-size: 16px;
  font-family: "微软雅黑";
}
</style>